# Comment/uncomment the following line to disable/enable debugging
DEBUG = y
MODULENAME = mapmod

ifneq ($(KERNELRELEASE),)

MODULE_SOURCES := main.o chdev.o fileop.o

ifeq ($(DEBUG),y)
	DEBFLAGS = -O -g -DMAPMOD_DEBUG # "-O" is needed to expand inlines
else
 	DEBFLAGS = -O2
endif

ccflags-y += $(DEBFLAGS)

obj-m := $(MODULENAME).o
$(MODULENAME)-y := $(MODULE_SOURCES)

else
# normal makefile
PWD := $(shell pwd)
KVERSION := $(shell uname -r)
KDIR ?= /lib/modules/$(KVERSION)/build

ifeq ($(INSTALL_MOD_DIR),)
	MODULE_SUFFIX := extra
else
	MODULE_SUFFIX := $(INSTALL_MOD_DIR)
endif

default: clean
	$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean

install:
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install
	depmod -a
	install -m 644 -o 0 -g 0 ./81-$(MODULENAME).rules /etc/udev/rules.d
	udevadm control --reload-rules
	modprobe $(MODULENAME)

uninstall:
	modprobe -r $(MODULENAME)
	rm -fv /lib/modules/$(shell uname -r)/$(MODULE_SUFFIX)/$(MODULENAME).ko
	depmod -a
	rm -fv /etc/udev/rules.d/81-$(MODULENAME).rules
	udevadm control --reload-rules

endif